import { Notification , MessageBox} from "element-ui";
import { Toast , Dialog } from "vant";
import { setCookie , getCookies , removeCookie } from "./cookie";
import ua from "./ua";

/**是否为手机*/
export const isPhone = () => {
    let isPhone = false;
    return new Promise((resolve, reject) => {
        isPhone = document.documentElement.clientWidth <= 768;
        window.addEventListener('resize', () => {
            let width = document.documentElement.clientWidth;
            if (width <= 768) {
                isPhone = true;
            } else {
                isPhone = false;
            }
            resolve(isPhone);
        });
        resolve(isPhone);
    })
}

/**
 * 显示提示
 * @param {String} msg 提示信息
 * @param {String} type 提示类型
 * @param {Number} duration 提示时间
 * @param {Boolean} forbidClick 是否禁止点击
 * */
export const showToast = async (option) => {
    if(typeof option === 'string'){
        option = {
            msg : option
        }
    }
    if (!option.msg) return;
    let { msg = '正在处理', type = 'info', duration = 1500, forbidClick = true } = option;
    if (await isPhone()) {
        Toast({
            message: msg,
            forbidClick: forbidClick,
            duration: duration
        })
        return
    }
    Notification({
        message: msg,
        type: type,
        duration: duration
    })
};


/**
 * 显示确认弹窗
 * @param {Object | String} option 配置项/提示信息
 * @param {String} option.title 标题
 * @param {String} option.message 提示信息
 * @param {String} option.type 类型
 * @param {String} option.confirmButtonText 确认按钮文本
 * @param {String} option.cancelButtonText 取消按钮文本
 * @param {Function} option.success 成功回调
 * @param {Function} option.cancel 取消回调
 * */ 
export const showConfirm = async (option = {}) => {
    let options = {
        title: '提示',
        message: '是否确认操作',
        type: '',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: () => {},
        cancel: () => {}
    }
    if(typeof option === 'string'){
        options.message = option;
    }
    Object.assign(options, option);
    if (await isPhone()) {
        Dialog.confirm({
            title: options.title,
            message: options.message,
            confirmButtonText: options.confirmButtonText,
            cancelButtonText: options.cancelButtonText
        }).then(() => {
            options.success();
        }).catch(() => {
            options.cancel();
        });
        return
    }
    MessageBox.confirm(options.message, options.title, {
        confirmButtonText: options.confirmButtonText,
        cancelButtonText: options.cancelButtonText,
        type: options.type
    }).then(() => {
        options.success();
    }).catch(() => {
        options.cancel();
    });
};

/**
 * 导出的方法
 * */
export {
    setCookie,
    getCookies,
    removeCookie,
    ua,
}
